<template>
  <div class="missing-container">
    <div class="header">
      <h2>失踪人口管理</h2>
      <el-button type="primary" @click="handleAdd">新增失踪人口信息</el-button>
    </div>

    <!-- 搜索框 -->
    <div class="search-container">
      <el-input v-model="searchName" placeholder="姓名" style="width: 200px; margin-right: 10px;"></el-input>
      <el-input-number v-model="searchAge" placeholder="年龄" :min="1" style="width: 120px; margin-right: 10px;"></el-input-number>
      <el-input v-model="searchPlace" placeholder="失踪地点" style="width: 200px;"></el-input>
      <el-button type="primary" @click="handleSearch">搜索</el-button>
    </div>

    <el-table :data="filteredMissingList" border style="width: 100%">
      <el-table-column prop="name" label="姓名" width="120"/>
      <el-table-column prop="gender" label="性别" width="80"/>
      <el-table-column prop="age" label="年龄" width="80"/>
      <el-table-column prop="missingDate" label="失踪日期" width="120"/>
      <el-table-column prop="missingPlace" label="失踪地点"/>
      <el-table-column prop="status" label="状态" width="100"/>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="50%">
      <el-form :model="form" label-width="100px">
        <el-form-item label="姓名">
          <el-input v-model="form.name"/>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.gender">
            <el-option label="男" value="男"/>
            <el-option label="女" value="女"/>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input-number v-model="form.age" :min="1"/>
        </el-form-item>
        <el-form-item label="失踪日期">
          <el-date-picker v-model="form.missingDate" type="date"/>
        </el-form-item>
        <el-form-item label="失踪地点">
          <el-input v-model="form.missingPlace"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status">
            <el-option label="待处理" value="待处理"/>
            <el-option label="处理中" value="处理中"/>
            <el-option label="已找到" value="已找到"/>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted, computed } from 'vue'

const missingList = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('新增失踪人口信息')

const form = reactive({
  name: '',
  gender: '男',
  age: 1,
  missingDate: '',
  missingPlace: '',
  status: '待处理'
})

// 搜索相关的状态
const searchName = ref('')
const searchAge = ref(null)
const searchPlace = ref('')

// 生成随机数据的函数
const generateRandomData = () => {
  const names = ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十', '郑十一', '冯十二']
  const genders = ['男', '女']
  const statuses = ['待处理', '处理中', '已找到']
  
  return Array.from({ length: 10 }, () => ({
    name: names[Math.floor(Math.random() * names.length)],
    gender: genders[Math.floor(Math.random() * genders.length)],
    age: Math.floor(Math.random() * 60) + 1, // 随机年龄 1-60
    missingDate: new Date(Date.now() - Math.floor(Math.random() * 10000000000)).toLocaleDateString(), // 随机日期
    missingPlace: '某地',
    status: statuses[Math.floor(Math.random() * statuses.length)],
  }))
}

// 计算属性用于过滤数据
const filteredMissingList = computed(() => {
  return missingList.value.filter(item => {
    const matchesName = item.name.includes(searchName.value)
    const matchesAge = searchAge.value ? item.age === searchAge.value : true
    const matchesPlace = item.missingPlace.includes(searchPlace.value)
    return matchesName && matchesAge && matchesPlace
  })
})

// 在组件加载时生成随机数据
onMounted(() => {
  missingList.value = generateRandomData()
})

const handleAdd = () => {
  dialogTitle.value = '新增失踪人口信息'
  dialogVisible.value = true
}

const handleEdit = (row: any) => {
  dialogTitle.value = '编辑失踪人口信息'
  Object.assign(form, row)
  dialogVisible.value = true
}

const handleDelete = (row: any) => {
  // TODO: 实现删除逻辑
}

const handleSubmit = () => {
  // TODO: 实现提交逻辑
  dialogVisible.value = false
}

const handleSearch = () => {
  // 触发计算属性更新
}
</script>

<style scoped>
.missing-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header h2 {
  margin: 0;
}

.search-container {
  margin-bottom: 20px;
}
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>
